from django.utils import timezone
from datetime import datetime
import os
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured

# 设置环境变量
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'application.settings')

# 获取当前时区（Django配置的时区）
current_tz = timezone.get_current_timezone()

# 将字符串时间转换为 datetime 对象
start_time_str = '2025-01-07 15:37:00'
end_time_str = '2025-01-16 19:50:00'

# 解析字符串为 datetime 对象
start_time = datetime.strptime(start_time_str, '%Y-%m-%d %H:%M:%S')
end_time = datetime.strptime(end_time_str, '%Y-%m-%d %H:%M:%S')

# 将 exam.start_time 和 exam.end_time 转换为 aware datetime
aware_start_time = timezone.make_aware(start_time, timezone=current_tz)
aware_end_time = timezone.make_aware(end_time, timezone=current_tz)

now = timezone.now()

time_left = aware_end_time - now
total_seconds_left = time_left.days * 24 * 3600 + time_left.seconds  # 总秒数

hours_left = total_seconds_left // 3600  # 小时数
remaining_seconds = total_seconds_left % 3600
minutes_left = remaining_seconds // 60  # 分钟数
seconds_left = remaining_seconds % 60  # 秒数

print(f'剩余时间：{hours_left} 小时 {minutes_left} 分钟 {seconds_left} 秒')
